package com.lh0811.forge.dependency.security.server.authentication.impl;

import com.lh0811.forge.dependency.security.core.api.authentication.SecurityVoucherVerificationProcess;
import com.lh0811.forge.dependency.security.core.api.voucher.SecurityVoucher;
import com.lh0811.forge.dependency.security.core.model.entity.SecurityAuthority;
import com.lh0811.forge.dependency.security.core.model.param.TokenParam;
import com.lh0811.forge.dependency.security.core.model.vo.AuthenticationVo;
import com.lh0811.forge.dependency.security.core.token.TokenManager;
import com.lh0811.forge.dependency.security.server.SecurityServer;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

/**
 * @author lh0811
 * @date 2021/5/23
 */
@Slf4j
public class DefaultSecurityServerImpl implements SecurityServer {


    @Resource
    private SecurityVoucherVerificationProcess process;

    @Resource
    private TokenManager tokenManager;


    /**
     * 授权服务 用户授权
     */
    @Override
    public AuthenticationVo authorize(SecurityVoucher param) throws Exception {
        // 调用认证流程接口，对用户凭证进行认证
        SecurityAuthority securityAuthority = process.voucherVerificationProcess(param);
        // 认证完成后，组装返回给客户端的Token
        AuthenticationVo authenticationVo = new AuthenticationVo();
        authenticationVo.setToken(securityAuthority.getToken());
        return authenticationVo;
    }


    /**
     * 授权服务 销毁token
     */
    @Override
    public void tokenDestroy(TokenParam param) throws Exception {
        tokenManager.removeToken(param.getToken());
    }

}
